﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Aulac.Rsc.Ems.Tags;
using Aulac.Global.Common;
using Aulac.Rsc.Ems;
using System.Data;
using Aulac.Global.Data;
using System.Data.Common;

namespace Aulac.Srv.Db.Data.Ems.MdcFiles
{
    class MdcFileInfoDb
    {
           private Aulac.Srv.Db.Provider.IDbDataProvider mDbPrd = null;
        private DbCommand mDbCmd = null;
        public MdcFileInfoDb(Aulac.Srv.Db.Provider.IDbDataProvider pDbPrd)
        {
            mDbPrd = pDbPrd;
            mDbCmd = mDbPrd.DbConnectionEngine.CreateCommand();
        }

        /// <summary>
        /// Lay thong tin co ban cua Bn phuc vu tinh gia vien phi
        /// </summary>
        /// <param name="pMdcFilePrkID">ID Benh an</param>
        /// <param name="pPntRdcObjPrkID">ID doi tuong mien giam</param>
        /// <returns></returns>
        public MdcFilePntProfile GetProfilePntDb(string pMdcFilePrkID, string pPntRdcObjPrkID)
        {
            /*** Other Write: Thangnc ***/
            /*** Code Date: 09/17/2010 ***/

            string[] arrH = new string[] { tagMdcFiles.MdcFiles.MdcFilePrkID, tagMdcFiles.MdcFiles.PntPrkID, tagMdcFiles.MdcFiles.IsKcbDgTuyen, tagMdcFiles.MdcFiles.IsPntEmergency, tagMdcFiles.MdcFiles.FncStylePntPrkID };
            string[] arrL = new string[] { tagMdcFiles.MdcFiles.PntRdcObjPrkID, tagMdcFiles.MdcFiles.IsValidIns };

            string sql = string.Format("SELECT h.{0}, l.{1} ", StringHelper.ConvertArrayToString(", h.", arrH), StringHelper.ConvertArrayToString(", l.", arrL));
            sql += string.Format(" FROM {0} h INNER JOIN {1} l ", TableNames.MdcFile_PntExam, TableNames.MdcFile_PntRdcObj);
            sql += string.Format(" ON h.{0} = l.{0}", tagMdcFiles.MdcFiles.MdcFilePrkID);
            sql += string.Format(" WHERE (h.{0} = ? ) ", tagMdcFiles.MdcFiles.MdcFilePrkID);
            mDbPrd.AddParamToCmd(mDbCmd, pMdcFilePrkID);
            if (ValueHelper.isEmpty(pPntRdcObjPrkID))
            {
                sql += string.Format(" AND (l.{0} = 1) ", tagMdcFiles.MdcFiles.IsActive);
                //mDbPrd.AddParamToCmd(mDbCmd, false, true);
            }
            else
            {
                sql += string.Format("(l.{0} = ?)", tagMdcFiles.PntRdcObj.PntRdcObjPrkID);
                mDbPrd.AddParamToCmd(mDbCmd, false, pPntRdcObjPrkID);
            }
            sql += string.Format("GROUP BY h.{0}, l.{1} ", StringHelper.ConvertArrayToString(", h.", arrH), StringHelper.ConvertArrayToString(", l.", arrL));

            DataTable tb = mDbPrd.ExecuteReaderDataToTable(mDbCmd, sql);
            if (!ValueHelper.isEmpty(tb))
            {
                return new MdcFilePntProfile()
                {
                    FncStylePntPrkID = DataTableProcess.GetValue(tb.Rows[0], tagMdcFiles.MdcFiles.FncStylePntPrkID),
                    IsKcbDgTuyen = DataTableProcess.GetValueBoolean(tb.Rows[0], tagMdcFiles.MdcFiles.IsKcbDgTuyen),
                    IsPntEmergency = DataTableProcess.GetValueBoolean(tb.Rows[0], tagMdcFiles.MdcFiles.IsPntEmergency),
                    IsValidIns = DataTableProcess.GetValueBoolean(tb.Rows[0], tagMdcFiles.PntRdcObj.IsValidIns),
                    MdcFilePrkID = DataTableProcess.GetValue(tb.Rows[0], tagMdcFiles.MdcFiles.MdcFilePrkID),
                    PntPrkID = DataTableProcess.GetValue(tb.Rows[0], tagMdcFiles.MdcFiles.PntPrkID),
                    PntRdcObjPrkID = DataTableProcess.GetValue(tb.Rows[0], tagMdcFiles.PntRdcObj.PntRdcObjPrkID)
                };
            }
            return null;
        }
    }
}
